package b3.model
{
	import b3.config.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.home.HomeScene;
	import b3.scene.home.grid.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.card.*;
	import b3.widget.home.operation.*;
	import b3.widget.home.town.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class CardModel extends Model
	{
		public var objectIndex:int;
		public var objectNum:int;
		private var cardData:Object;
		
		public function CardModel(objIndex:int, objNum:int)
		{
			super();
			objectIndex = objIndex;
			objectNum = objNum;
			cardData = new Object();
			cardData['objectIndex'] = objIndex;
			cardData['objectNum'] = objNum;
		 	var cardConfig:CardConfig = Config.objConfigs[objectIndex];
		 	cardData['storeTabIndex'] = cardConfig.storeTabIndex;
		 	cardData['effectValue'] = cardConfig.effectValue;
		}
		
		//首先是针对不需要选择目标的卡片的处理
		public function useCard():void
		{
			switch (objectIndex)
			{
				case CardConfig.STABLE_SMALL:
				case CardConfig.STABLE_BIG:
				case CardConfig.INCREASE_SMALL:
				case CardConfig.INCREASE_BIG:
				case CardConfig.SAFETY_SMALL:
				case CardConfig.SAFETY_BIG:
				case CardConfig.SAFETY_SMALL:
				case CardConfig.SAFETY_BIG:
				case CardConfig.DELAY_SMALL:
				case CardConfig.DELAY_BIG:
					useStateCard();
				break;
				case CardConfig.VITALITY_SMALL:
				case CardConfig.VITALITY_BIG:
					useVitalityCard();
				break;
				case CardConfig.TAX_INSPECT:
					useTaxInspectionCard();
				break;
			}
			HomeScene.getInstance().startToUseCard(this);
		}
		
		//设置建筑对象
		public function setTarget(buildingModel:BuildingModel):void
		{
			var buildingConfig:BuildingConfig = Config.buildingConfigs[buildingModel.buildingIndex];
			buildingModel.buildingType = buildingConfig.type;
			cardData['buildingModel'] = buildingModel;
			switch (objectIndex)
			{
				case CardConfig.ALTERATION:
					var selectPane:ChangeTypePane = new ChangeTypePane(this, buildingModel);
					WidgetManager.getInstance().popOut([selectPane]);
				break;
				case CardConfig.PURCHASE:
					HomeScene.getInstance().tempBuildingModel = buildingModel.copy();
					PlayerManager.getInstance().me.townModel.addEventListener(ModelEvent.REFRESH, HomeScene.getInstance().startToPlace);
					HomeScene.getInstance().loadPlayer(PlayerManager.getInstance().me);
				break;
				case CardConfig.ANGEL_SMALL:
				case CardConfig.ANGEL_BIG:
				case CardConfig.DEVIL_SMALL:
				case CardConfig.DEVIL_BIG:
					useAttackBuildingCard();
				break;
				case CardConfig.DESTRUCTION:
					useDestructionCard();	
				break;
				case CardConfig.VIRUS:
				case CardConfig.POISON_GAS:
				case CardConfig.ROCKET_BOMB:
				case CardConfig.NUCLEAR_BOMB:
					useAttackAreaCard();
				break;
				case CardConfig.TAX_INSPECT:
					useTaxInspectionCard();
				break;
				case CardConfig.SEAL:
					useSealCard();
				break;
				case CardConfig.PROTECT:
					useProtectCard();
				break;
			}
		}
		
		 /**
		 *	处理返回的物品使用事件  需要同时更新物品信息和状态信息
		 **/
		 public function checkFail(data:Object):Boolean
		 {
		 	var event:ModelEvent;
		 	if (data['err'] == 0)
		 	{
			 	//使用成功
				event = new ModelEvent(ModelEvent.CARD_USE_SUCCESS);
		 		event.data = data;
		 		dispatchEvent(event);
		 		return false;
		 	}
		 	else
		 	{
		 		event = new ModelEvent(ModelEvent.CARD_USE_FAIL);
		 		event.data = data;
		 		dispatchEvent(event);
		 		return true;
		 	}
		 }
		 /**
		 * 状态卡
		 * */
		 public function useStateCard():void
		 {
		 	cardData['state'] = Config.objConfigs[ objectIndex ].state;
		 	cardData['times'] = Config.objConfigs[ objectIndex ].times;
		 	cardData['oppositeState'] = (Config.objConfigs[ objectIndex ] as CardConfig).oppositeState;
		 	
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.STATE_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleStateCard);
			serverRequest.post(); 
		 }
		 public function handleStateCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;
		 	//如果发生互斥
			if( data['exclusion'] )
			{
				 var modelEvent:ModelEvent = new ModelEvent( ModelEvent.CARD_EXCLUSION);
				 dispatchEvent( modelEvent );
			}
			else
			{
				 //如果使用的卡片属于第一类 次数卡片
				 PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
				 PlayerManager.getInstance().me.stateModel.handleLoadStates(data['state']);
				 HomeScene.getInstance().playerPane.statePane.playAddStateEffect(data['stateIndex']);
			}
		 }

		 /**
		 * 大小体力卡
		 * */
		 public function useVitalityCard():void
		 {
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.VITALITY_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleVitalityCard);
			serverRequest.post();
		 }
		 public function handleVitalityCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;
		 	if (data["vitalityZeroTime"] > 0)
			{
				var me:Player = PlayerManager.getInstance().me;
				me.vitalityZeroTime = data["vitalityZeroTime"];
			}
			PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
			var event:ModelEvent = new ModelEvent(ModelEvent.CARD_USE_SUCCESS);
	 		event.data = data;
	 		dispatchEvent(event);
		 }
		 /**
		 * 大小天使卡 大小恶魔卡
		 * */
		 public function useAttackBuildingCard():void
		 {
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.ATTACK_BUILDING_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleAttackBuildingCard);
			serverRequest.post();
		 }
		 public function handleAttackBuildingCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;		 	
		 	HomeScene.getInstance().getCurrentTown().owner.townModel.handleRebuild(data);
		 	PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
		 }

		 /**
		 * 拆除卡
		 * */
		 public function useDestructionCard():void
		 {
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.DESTRUCTION_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleDestructionCard);
			serverRequest.post();
		 }
		 
		 public function handleDestructionCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;		 	
		 	PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
		 	HomeScene.getInstance().getCurrentTown().owner.townModel.handleRemoveBuilding(data);
		 	HomeScene.getInstance().getCurrentTown().owner.townModel.dispatchEvent(new ModelEvent(ModelEvent.REFRESH));
		 		
		 }

		 /**
		 * 改建卡
		 * */
		 public function useAlterationCard(newBuildingIndex:int):void
		 {
			cardData['newBuildingIndex'] = newBuildingIndex;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.ALTERATION_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleAlterationCard);
			serverRequest.post();
		 }
		 public function handleAlterationCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;		 	
		 	HomeScene.getInstance().getCurrentTown().owner.townModel.handleRebuild(data);
		 	PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
		 }
		 /**
		 * 飞弹卡 原子弹卡 毒气卡 细菌卡
		 * */
		 public function useAttackAreaCard():void
		 {
	 		//这里需要上传一个特殊的所有者信息  亦即该建筑物对应的用户ID 不同于使用卡片者的ID
	 		cardData['buildingOwnerId'] = HomeScene.getInstance().getCurrentTown().owner.userId;
	 		cardData['effectValue'] = (Config.objConfigs[ objectIndex ] as CardConfig).effectValue;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.ATTACK_AREA_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleAttackAreaCard);
			serverRequest.post();
		 }
		 public function handleAttackAreaCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if (checkFail(data)) return;
		 	//更新建筑
		 	var townModel:TownModel = HomeScene.getInstance().getCurrentTown().owner.townModel;
		 	townModel.attackArea(data);
		 	//更新包裹
		 	PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
		 }
		 
		 /**
		 * 查税卡
		 * */
		 public function useTaxInspectionCard():void
		 {
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.TAX_INSPECTION_CARD, {obj:cardData, ownerId:HomeScene.getInstance().getCurrentTown().owner.userId});
			serverRequest.setHandleFunc(handleTaxInspectionCard);
			serverRequest.post();
		 }
		 public function handleTaxInspectionCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;
		 	//更新包裹		 	
		 	PlayerManager.getInstance().me.packageModel.handleLoadObjects(data['package']);
		 }
		 /**
		 * 查封卡
		 * */
		 public function useSealCard():void
		 {
		 	cardData['townOwnerId'] = HomeScene.getInstance().getCurrentTown().owner.userId;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.SEAL_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleSealCard);
			serverRequest.post();
		 }
		 
		 public function handleSealCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;
		 	//更新自身包裹		 	
		 	var me:Player = PlayerManager.getInstance().me;
		 	me.packageModel.handleLoadObjects(data['package']);
		 	//更新所有者状态
		 	var ownerId:int = data['state']['ownerId'];
		 	var owner:Player = PlayerManager.getInstance().players[ownerId];
		 	owner.stateModel.handleLoadStates(data['state']);
		 }
		 /**
		 * 保护卡
		 * */
		 public function useProtectCard():void
		 {
		 	cardData['townOwnerId'] = HomeScene.getInstance().getCurrentTown().owner;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.PROTECT_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleProtectCard);
			serverRequest.post();
		 }
		 public function handleProtectCard(data:Object):void
		 {	 	
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;
		 	//更新自身包裹		 	
		 	var me:Player = PlayerManager.getInstance().me;
		 	me.packageModel.handleLoadObjects(data['package']);
		 	//更新所有者状态
		 	var ownerId:int = data['state']['ownerId'];
		 	var owner:Player = PlayerManager.getInstance().players[ownerId];
		 	owner.stateModel.handleLoadStates(data['state']);
		 }

		 /**
		 * 购房卡
		 * */
		 public function useBuyBuildingCard(buildingModel:BuildingModel):void
		 {
		 	cardData['newBuildingModel'] = buildingModel;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.BUY_BUILDING_CARD, {obj:cardData});
			serverRequest.setHandleFunc(handleBuyBuildingCard);
			serverRequest.post();
		 }
		 
		 private function handleBuyBuildingCard(data:Object):void
		 {
		 	//检查是否成功使用卡片
		 	if(checkFail(data)) return;		 	
		 	var me:Player = PlayerManager.getInstance().me;
		 	me.townModel.handleAddBuilding(data);
		 	me.packageModel.handleLoadObjects(data['package']);
		 }
	}
}